package org.yang.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.yang.reggie.common.BaseContext;
import org.yang.reggie.common.R;
import org.yang.reggie.entity.ShoppingCart;
import org.yang.reggie.service.ShoppingCartService;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 缘友一世
 * date 2023/7/13-9:07
 */
@RestController
@Slf4j
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 购物车添加功能
     * @param shoppingCart
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据：{}",shoppingCart);

        //设置用户id，指定该用户的购物车数据
        Long currentId = BaseContext.getCurrentId();//1678961550957682689
        shoppingCart.setUserId(currentId);

        //构造查询条件
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        /*查询当前菜品或套餐是否在购物车中
          如果存在，数量+1
          如果不存在，添加到购物车，数量默认为1
         */

        //(dish_id = ? OR setmeal_id = ?)的查询条件的构造
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null) {
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询当前菜品或套餐是否在购物车中
        //SELECT * FROM shopping_cart WHERE user_id = ? AND (dish_id = ? OR setmeal_id = ?)
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if(cartServiceOne!=null) {
            //如果存在，数量+1
            Integer number = cartServiceOne.getNumber();
            //后置自增操作符 number++是错误的 应使用前置自增操作符 ++number
            cartServiceOne.setNumber(++number);
            shoppingCartService.updateById(cartServiceOne);
        }else {
            //如果不存在，添加到购物车，数量默认为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne=shoppingCart;
        }

        return R.success(cartServiceOne);
    }

    /**
     * 查看购物车
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        log.info("查看购物车...");

        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);

        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);

        return R.success(list);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        log.info("购物车数据：{}",shoppingCart);

        //设置用户id，指定该用户的购物车数据
        Long currentId = BaseContext.getCurrentId();//1678961550957682689
        shoppingCart.setUserId(currentId);

        //构造查询条件
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);

        /*查询当前菜品或套餐
          如果存在，数量-1
         */

        //(dish_id = ? OR setmeal_id = ?)的查询条件的构造
        Long dishId = shoppingCart.getDishId();
        if(dishId!=null) {
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else {
            //添加到购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询当前菜品或套餐是否在购物车中
        //SELECT * FROM shopping_cart WHERE user_id = ? AND (dish_id = ? OR setmeal_id = ?)
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);
        //存在，数量-1
        Integer number = cartServiceOne.getNumber();
        //前置自减操作符 --number
        cartServiceOne.setNumber(--number);
        //如果数量为0，执行删除操作，否则执行更新
        if(number==0) {
            shoppingCartService.removeById(cartServiceOne);
        }else {
            shoppingCartService.updateById(cartServiceOne);
        }

        return R.success(cartServiceOne);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }
}
